﻿using System;
using System.Globalization;

namespace KPIS.GERP.Models.Class
{
	public static class ConvertUtility
	{
		/// <summary>
		/// Make string to bool, return default if error can't convert
		/// </summary>
		/// <param name="str">string param to convert</param>
		/// <returns>Converted value, default (false) if null</returns>
		public static bool ConvertOrDefaultStringToBool(string str)
		{
			bool result;
			bool success = Boolean.TryParse(str, out result);
			return success && result;
		}

		/// <summary>
		/// Make string to int, return default if error can't convert
		/// </summary>
		/// <param name="str">string param to convert</param>
		/// <returns>Converted value, default (0) if null</returns>
		public static int ConvertOrDefaultStringToInt(string str)
		{
			int result;
			Int32.TryParse(str, out result);
			return result;
		}

		/// <summary>
		/// Make string to float, return default if error can't convert
		/// </summary>
		/// <param name="str">string param to convert</param>
		/// <returns>Converted value, default (0.0) if null</returns>
		public static float ConvertOrDefaultStringToFloat(string str)
		{
			float result;
			float.TryParse(str, NumberStyles.Float, CultureInfo.InvariantCulture, out result);
			return result;
		}
		
		/// <summary>
		/// Make string to string lower case, return empty string if it null
		/// </summary>
		/// <param name="str">string param to convert</param>
		/// <returns>String without null and lower case all</returns>
		public static string MakeStringEmptyAndLower(string str)
		{
			return (str ?? string.Empty).ToLower().Trim();
		}

		/// <summary>
		/// Make any object to bool, return default if error can't convert
		/// </summary>
		/// <param name="str">string param to convert</param>
		/// <returns>Converted value, default (false) if null</returns>
		public static bool ConvertOrDefaultObjectToBool(object str)
		{
			bool result;
			bool success = Boolean.TryParse(MakeObjectToStringDefaultEmpty(str), out result);
			return success && result;
		}

		/// <summary>
		/// Make any object to int, return default if error can't convert
		/// </summary>
		/// <param name="str">string param to convert</param>
		/// <returns>Converted value, default (0) if null</returns>
		public static int ConvertOrDefaultObjectInt(object str)
		{
			int result;
			Int32.TryParse(MakeObjectToStringDefaultEmpty(str), out result);
			return result;
		}

		/// <summary>
		/// Make any object to float, return default if error can't convert
		/// </summary>
		/// <param name="str">string param to convert</param>
		/// <returns>Converted value, default (0.0) if null</returns>
		public static float ConvertOrDefaultObjectFloat(object str)
		{
			float result;
			float.TryParse(MakeObjectToStringDefaultEmpty(str), NumberStyles.Float, CultureInfo.InvariantCulture, out result);
			return result;
		}

        public static TimeSpan ConvertOrDefaultObjectTimeSpan(object str)
        {
            TimeSpan result;
            TimeSpan.TryParse(MakeObjectToStringDefaultEmpty(str), out result);
            return result;
        }

		/// <summary>
		/// Make object to string lower case, return empty string if it null
		/// </summary>
		/// <param name="str">string param to convert</param>
		/// <returns>String without null and lower case all</returns>
		public static string MakeObjectToStringEmptyAndLower(object str)
		{
			return MakeObjectToStringDefaultEmpty(str).ToLower().Trim();
		}

		/// <summary>
		/// Make object to string, return empty string if it null
		/// </summary>
		/// <param name="str">string param to convert</param>
		/// <returns>String without null</returns>
		public static string MakeObjectToStringDefaultEmpty(object str)
		{
			return (str == null ? string.Empty : str.ToString()).Trim();
		}

        public static string LimitCharactor(string text = "", int limitLenght = 50)
        {
            return text.Substring(0, Math.Min(limitLenght, text.Length)) + (text.Length > limitLenght ? "..." : string.Empty);
        }
        public static DateTime ConvertOrDefaultToDateTime(object obj)
        {
            DateTime result;
            DateTime.TryParse((obj ?? new object()).ToString(), out result);
            return result;
        }
	}
}
